package com.hansheng.action.chooseSheet;

import com.alibaba.fastjson.JSONArray;
import com.hansheng.entity.OutFormEntity;
import com.hansheng.service.impl.SemiProductServiceImpl;
import com.hansheng.service.impl.SheetServiceImpl;
import com.hansheng.util.JsonHelper;
import com.hansheng.util.PrintWriterHelper;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.hansheng.util.JsonHelper.OPER_FAILED;

/**
 * Created by GoldenMan on 2017/5/12.
 */
public class SaveOutFormAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
    private SheetServiceImpl sheetService;
    private HttpServletResponse httpServletResponse;
    private HttpServletRequest httpServletRequest;
    private String cutMaterialNo;
    private String strSheetDetails;
    private String strSemiProductDetails;
    private int operator;
    private SemiProductServiceImpl semiProductService;

    public void setSheetService(SheetServiceImpl sheetService) {
        this.sheetService = sheetService;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.httpServletResponse = response;
        httpServletResponse.setContentType("text/html;charset=utf-8");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
    }

    public String getStrSheetDetails() {
        return strSheetDetails;
    }

    public void setStrSheetDetails(String strSheetDetails) {
        this.strSheetDetails = strSheetDetails;
    }

    public String getStrSemiProductDetails() {
        return strSemiProductDetails;
    }

    public void setStrSemiProductDetails(String strSemiProductDetails) {
        this.strSemiProductDetails = strSemiProductDetails;
    }

    public String getCutMaterialNo() {
        return cutMaterialNo;
    }

    public void setCutMaterialNo(String cutMaterialNo) {
        this.cutMaterialNo = cutMaterialNo;
    }

    public int getOperator() {
        return operator;
    }

    public void setOperator(int operator) {
        this.operator = operator;
    }


    @Override
    public String execute() throws Exception {
        OutFormEntity outFormEntity = new OutFormEntity();
        outFormEntity.setOutFormNo(getOutFormNo(getCutMaterialNo()));
        outFormEntity.setOperator(getOperator());
        boolean flag = sheetService.saveOutForm(outFormEntity, JSONArray.parseArray(getStrSheetDetails())) && semiProductService.saveSemiProduct(outFormEntity, JSONArray.parseArray(getStrSemiProductDetails()));
        PrintWriterHelper.send(flag? JsonHelper.successJSONBuilder():JsonHelper.errJSONBulider(1,OPER_FAILED));
        return null;
    }

    public void setSemiProductService(SemiProductServiceImpl semiProductService) {
        this.semiProductService = semiProductService;
    }

    private String getOutFormNo(String cutMaterialNo) {
        int counter = sheetService.getOutFormNoCounter(cutMaterialNo);//
        return cutMaterialNo + counter;
    }
}
